package com.yjc.hire.work.service.impl;

import com.yjc.enums.DealStatus;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.work.bo.SearchReportJobBO;
import com.yjc.hire.work.repository.ReportJobRepository;
import com.yjc.hire.work.service.ReportService;
import com.yjc.mo.ReportMO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Pattern;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/9/3
 * </p>
 */

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportJobRepository reportJobRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveReportRecord(ReportMO reportMO) {
        reportMO.setDealStatus(DealStatus.WAITING.type);
        reportMO.setCreatedTime(LocalDateTime.now());
        reportMO.setUpdatedTime(LocalDateTime.now());
        reportJobRepository.save(reportMO);
    }

    @Override
    public boolean isReportRecordExist(String reportId, String jobId) {
        ReportMO repostMo = reportJobRepository.findByReportUserIdAndJobId(reportId, jobId);
        return repostMo != null;
    }

    @Override
    public PageGridResult<List<ReportMO>> pageReportRecordList(SearchReportJobBO searchReportJobBO, Integer page, Integer pageSize) {
        String jobName = searchReportJobBO.getJobName();
        String companyName = searchReportJobBO.getCompanyName();
        String reportUserName = searchReportJobBO.getReportUserName();
        Integer dealStatus = searchReportJobBO.getDealStatus();
        LocalDateTime beginDateTime = searchReportJobBO.getBeginDateTime();
        LocalDateTime endDateTime = searchReportJobBO.getEndDateTime();
        Query query = new Query();
        Criteria criteria = new Criteria();
        if (StringUtils.isNotBlank(jobName)) {
            query = addLikeByValue(query, "job_ame", jobName);
        }
        if (StringUtils.isNotBlank(companyName)) {
            query = addLikeByValue(query, "company_name", companyName);
        }
        if (StringUtils.isNotBlank(reportUserName)) {
            query = addLikeByValue(query, "report_user_name", reportUserName);
        }
        if (dealStatus != null) {
            query.addCriteria(Criteria.where("deal_status").is(dealStatus));
        }
        if (beginDateTime != null) {
            query.addCriteria(Criteria.where("create_time").gte(beginDateTime));
        }
        if (endDateTime != null) {
            query.addCriteria(Criteria.where("create_time").lte(endDateTime));
        }
        Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.DESC, "create_time");
        query.with(pageable);
        List<ReportMO> mos = mongoTemplate.find(query, ReportMO.class);
        return PageGridResult.of(mos,page);

    }

    private Query addLikeByValue(Query query,String key,String value){
        Pattern pattern = Pattern.compile("^.*" + value + ".*$");
        query.addCriteria(Criteria.where(key).regex(value));
        return query;
    }
}
